<?php

define("COMPANYTYPE_OWN", 1);
define("COMPANYSTATUS_USE", 1);
define("COMPANYTATUS_DELETE", 2);

/*
require_once '../config/conf.inc.php';
$ret = CompanyManager::check_register_mail("mayan_1104@yahoo.com.cn", "60e6ce88593c8648f22ff29992892eab", $err);
echo "ret=$ret\n";
echo "err=$err\n";
*/

Class CompanyManager
{
	static function get_email($user)
	{
		$err = "";	
		if (empty($user))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FEmail from Tbl_CompanyAccount where FAccount='$user' or FEmail='$user'";
		$db->query($sql);
		if ($db->is_error)
		{
			return false;
		}
		
		$res = $db->fetch_array();
		$email = $res['FEmail'];
		if (!empty($email))
		{
			return  $email;	
		}
		
		return false;
	}
	
	static function modify_email($account, $new_email, &$err)
	{
		$err = "";
		if (empty($account) || empty($new_email))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_CompanyAccount set FEmail='$new_email' where FAccount='$account' or FEmail='$account' limit 1";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function modify_password($account, $new_pass, $err)
	{
		$err = "";
		if (empty($account) || empty($new_pass))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_CompanyAccount set FPasswd=password('$new_pass') where FAccount='$account' or FEmail='$account' limit 1";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function set_user_status($uid, $status, &$err)
	{
		$err = "";
		if (empty($uid))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "update Tbl_CompanyAccount set FStatus=$status where FUId=$uid";
		$db->query($sql);
		$ret = $db->db_affected_rows();
		if ($ret != 1)
		{
			$err = "update_err";
			return false;
		}
		return true;
	}
	
	static function check_user_account($user, $email, &$err)
	{
		$err = "";	
		if (empty($user) || empty($email))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FUId from Tbl_CompanyAccount where FAccount='$user' or FEmail='$email'";
		$db->query($sql);
		if ($db->is_error)
		{
			return false;
		}
		
		$res = $db->fetch_array();
		$uid = $res['FUId'];
		if (!empty($uid))
		{
			return  true;	
		}
		
		return false;
	}
	
	static function check_login($user, $pass, $type, &$err)
	{
		$err = "";
		if (empty($user) || empty($pass) || empty($type))
		{
			$err = "params_empty";
			return false;
		}
		
		if ($type == USERTYPE_OWN)
		{
			return AccountManager::check_login_own($user, $pass, $err);
		}
		
		return false;
	}
	
	static function check_login_own($user, $pass, &$err)
	{
		$err = "";
		if (empty($user) || empty($pass))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FUId,FStatus from Tbl_CompanyAccount where FAccount='$user' and FPasswd=password('$pass')";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "query_err";
			return false;
		}
		
		$res = $db->fetch_array();
		$uid = $res['FUId'];
		$status = $res['FStatus'];
		if (empty($uid))	return false;
		
		return $status;
	}
	
	static function insert_user_account($user, $pass, $email, $type, &$err)
	{
		$err = "";
			
		if (empty($user) || empty($pass) || empty($email) || empty($type))
		{
			$err = "params_empty";
			return false;
		}
		
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Master", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql =  "insert into Tbl_CompanyAccount set FAccount='$user', FPasswd=password('$pass'), ".
			"FEmail='$email', FType=$type, FStatus=".USERSTATUS_CHECK;
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "insert_err";
			return false;
		}
		
		return true;
	}
};

?>
